// 模拟订单数据
const orderData = [
  {
    id: 'ORD20250728001',
    orderNumber: 'BT20250728001',
    status: 'pending',
    statusText: '待付款',
    date: '2025-07-28 14:30:22',
    totalAmount: '299.00',
    paymentDeadline: '2025-07-28 15:00:22',
    items: [
      {
        id: 'ITEM001',
        name: '高级矿机 Pro',
        image: 'https://via.placeholder.com/80',
        price: '299.00',
        quantity: 1,
        specs: '黑色 / 标准版'
      }
    ],
    countdown: 1800, // 30分钟倒计时（秒）
    recipient: {
      name: '张三',
      phone: '138****8000',
      address: '广东省深圳市南山区科技园南区T3栋502室'
    },
    paymentMethod: 'HDFS',
    orderSteps: [
      { title: '提交订单', time: '2025-07-28 14:30:22', completed: true },
      { title: '付款', time: '', completed: false },
      { title: '商家发货', time: '', completed: false },
      { title: '确认收货', time: '', completed: false },
      { title: '订单完成', time: '', completed: false }
    ]
  },
  {
    id: 'ORD20250727001',
    orderNumber: 'BT20250727001',
    status: 'paid',
    statusText: '待发货',
    date: '2025-07-27 09:15:36',
    totalAmount: '599.00',
    items: [
      {
        id: 'ITEM002',
        name: '超级矿机 Max',
        image: 'https://via.placeholder.com/80',
        price: '599.00',
        quantity: 1,
        specs: '银色 / 高级版'
      }
    ],
    recipient: {
      name: '张三',
      phone: '138****8000',
      address: '广东省深圳市南山区科技园南区T3栋502室'
    },
    paymentMethod: 'HDFS',
    orderSteps: [
      { title: '提交订单', time: '2025-07-27 09:15:36', completed: true },
      { title: '付款', time: '2025-07-27 09:16:22', completed: true },
      { title: '商家发货', time: '', completed: false },
      { title: '确认收货', time: '', completed: false },
      { title: '订单完成', time: '', completed: false }
    ]
  },
  {
    id: 'ORD20250726001',
    orderNumber: 'BT20250726001',
    status: 'shipped',
    statusText: '待收货',
    date: '2025-07-26 16:42:18',
    totalAmount: '199.00',
    items: [
      {
        id: 'ITEM003',
        name: '入门矿机 Mini',
        image: 'https://via.placeholder.com/80',
        price: '199.00',
        quantity: 1,
        specs: '白色 / 基础版'
      }
    ],
    recipient: {
      name: '张三',
      phone: '138****8000',
      address: '广东省深圳市南山区科技园南区T3栋502室'
    },
    logistics: {
      company: '顺丰速运',
      trackingNumber: 'SF1234567890123',
      status: '运输中',
      updates: [
        { time: '2025-07-27 10:30:00', content: '快件已由【深圳南山集散中心】发出，正在运往【广州集散中心】' },
        { time: '2025-07-27 08:15:00', content: '快件已到达【深圳南山集散中心】' },
        { time: '2025-07-26 20:45:00', content: '商家已发货' }
      ]
    },
    paymentMethod: 'HDFS',
    orderSteps: [
      { title: '提交订单', time: '2025-07-26 16:42:18', completed: true },
      { title: '付款', time: '2025-07-26 16:43:05', completed: true },
      { title: '商家发货', time: '2025-07-26 20:45:00', completed: true },
      { title: '确认收货', time: '', completed: false },
      { title: '订单完成', time: '', completed: false }
    ]
  },
  {
    id: 'ORD20250725001',
    orderNumber: 'BT20250725001',
    status: 'completed',
    statusText: '已完成',
    date: '2025-07-25 11:20:35',
    totalAmount: '899.00',
    items: [
      {
        id: 'ITEM004',
        name: '旗舰矿机 Ultra',
        image: 'https://via.placeholder.com/80',
        price: '899.00',
        quantity: 1,
        specs: '金色 / 豪华版'
      }
    ],
    recipient: {
      name: '张三',
      phone: '138****8000',
      address: '广东省深圳市南山区科技园南区T3栋502室'
    },
    logistics: {
      company: '中通快递',
      trackingNumber: 'ZT9876543210987',
      status: '已签收',
      updates: [
        { time: '2025-07-27 14:20:00', content: '客户已签收，签收人：张三，感谢您使用中通快递' },
        { time: '2025-07-27 09:30:00', content: '快件已到达【南山科技园营业点】，快递员【李师傅，电话：13900000000】正在为您派送' },
        { time: '2025-07-26 20:15:00', content: '快件已到达【深圳南山区】' },
        { time: '2025-07-26 08:45:00', content: '快件已由【上海集散中心】发出，正在运往【深圳集散中心】' },
        { time: '2025-07-25 16:30:00', content: '商家已发货' }
      ]
    },
    paymentMethod: 'HDFS',
    orderSteps: [
      { title: '提交订单', time: '2025-07-25 11:20:35', completed: true },
      { title: '付款', time: '2025-07-25 11:21:30', completed: true },
      { title: '商家发货', time: '2025-07-25 16:30:00', completed: true },
      { title: '确认收货', time: '2025-07-27 14:20:00', completed: true },
      { title: '订单完成', time: '2025-07-27 14:20:00', completed: true }
    ]
  },
  {
    id: 'ORD20250724001',
    orderNumber: 'BT20250724001',
    status: 'cancelled',
    statusText: '已取消',
    date: '2025-07-24 08:30:12',
    totalAmount: '299.00',
    items: [
      {
        id: 'ITEM001',
        name: '高级矿机 Pro',
        image: 'https://via.placeholder.com/80',
        price: '299.00',
        quantity: 1,
        specs: '黑色 / 标准版'
      }
    ],
    recipient: {
      name: '张三',
      phone: '138****8000',
      address: '广东省深圳市南山区科技园南区T3栋502室'
    },
    paymentMethod: 'HDFS',
    orderSteps: [
      { title: '提交订单', time: '2025-07-24 08:30:12', completed: true },
      { title: '付款', time: '', completed: false },
      { title: '商家发货', time: '', completed: false },
      { title: '确认收货', time: '', completed: false },
      { title: '订单完成', time: '', completed: false }
    ],
    cancelTime: '2025-07-24 09:15:30',
    cancelReason: '用户取消'
  }
]

// 订单详情数据
const orderDetailData = {
  '1001': {
    id: '1001',
    orderNumber: 'BT20250725001',
    product: 'Quantum Chronograph Watch',
    price: '1299.00',
    quantity: 1,
    totalAmount: '1299.00',
    status: 'pending',
    statusText: '待支付',
    date: '2025-07-25 14:30:22',
    paymentDeadline: '2025-07-26 14:30:22',
    image: 'https://images.unsplash.com/photo-1523275335684-37898b6baf30?q=80&w=300&auto=format&fit=crop',
    specs: '黑色表盘 / 不锈钢表带 / 42mm',
    recipient: {
      name: '张三',
      phone: '138****8000',
      address: '广东省深圳市南山区科技园南区T3栋502室'
    },
    logistics: null,
    paymentMethod: 'HDFS',
    orderSteps: [
      { title: '提交订单', time: '2025-07-25 14:30:22', completed: true },
      { title: '付款', time: '', completed: false },
      { title: '商家发货', time: '', completed: false },
      { title: '确认收货', time: '', completed: false },
      { title: '订单完成', time: '', completed: false }
    ]
  },
  '1002': {
    id: '1002',
    orderNumber: 'BT20250720002',
    product: 'Digital Smart Watch',
    price: '899.00',
    quantity: 1,
    totalAmount: '899.00',
    status: 'shipping',
    statusText: '待收货',
    date: '2025-07-20 09:15:36',
    paymentDeadline: null,
    image: 'https://images.unsplash.com/photo-1546868871-7041f2a55e12?q=80&w=300&auto=format&fit=crop',
    specs: '银色 / 硅胶表带 / 智能版',
    recipient: {
      name: '张三',
      phone: '138****8000',
      address: '广东省深圳市南山区科技园南区T3栋502室'
    },
    logistics: {
      company: '顺丰速运',
      trackingNumber: 'SF1234567890123',
      status: '运输中',
      updates: [
        { time: '2025-07-22 10:30:00', content: '快件已由【深圳南山集散中心】发出，正在运往【广州集散中心】' },
        { time: '2025-07-22 08:15:00', content: '快件已到达【深圳南山集散中心】' },
        { time: '2025-07-21 20:45:00', content: '商家已发货' }
      ]
    },
    paymentMethod: 'HDFS',
    orderSteps: [
      { title: '提交订单', time: '2025-07-20 09:15:36', completed: true },
      { title: '付款', time: '2025-07-20 09:16:22', completed: true },
      { title: '商家发货', time: '2025-07-21 20:45:00', completed: true },
      { title: '确认收货', time: '', completed: false },
      { title: '订单完成', time: '', completed: false }
    ]
  },
  '1003': {
    id: '1003',
    orderNumber: 'BT20250715003',
    product: 'Classic Analog Watch',
    price: '599.00',
    quantity: 1,
    totalAmount: '599.00',
    status: 'completed',
    statusText: '已完成',
    date: '2025-07-15 16:42:18',
    paymentDeadline: null,
    image: 'https://images.unsplash.com/photo-1524805444758-089113d48a6d?q=80&w=300&auto=format&fit=crop',
    specs: '棕色表盘 / 真皮表带 / 40mm',
    recipient: {
      name: '张三',
      phone: '138****8000',
      address: '广东省深圳市南山区科技园南区T3栋502室'
    },
    logistics: {
      company: '中通快递',
      trackingNumber: 'ZT9876543210987',
      status: '已签收',
      updates: [
        { time: '2025-07-18 14:20:00', content: '客户已签收，签收人：张三，感谢您使用中通快递' },
        { time: '2025-07-18 09:30:00', content: '快件已到达【南山科技园营业点】，快递员【李师傅，电话：13900000000】正在为您派送' },
        { time: '2025-07-17 20:15:00', content: '快件已到达【深圳南山区】' },
        { time: '2025-07-16 08:45:00', content: '快件已由【上海集散中心】发出，正在运往【深圳集散中心】' },
        { time: '2025-07-16 06:30:00', content: '商家已发货' }
      ]
    },
    paymentMethod: 'HDFS',
    orderSteps: [
      { title: '提交订单', time: '2025-07-15 16:42:18', completed: true },
      { title: '付款', time: '2025-07-15 16:43:05', completed: true },
      { title: '商家发货', time: '2025-07-16 06:30:00', completed: true },
      { title: '确认收货', time: '2025-07-18 14:20:00', completed: true },
      { title: '订单完成', time: '2025-07-18 14:20:00', completed: true }
    ]
  },
  '1004': {
    id: '1004',
    orderNumber: 'BT20250710004',
    product: 'Sport Fitness Watch',
    price: '499.00',
    quantity: 2,
    totalAmount: '998.00',
    status: 'completed',
    statusText: '已完成',
    date: '2025-07-10 11:23:45',
    paymentDeadline: null,
    image: 'https://images.unsplash.com/photo-1508685096489-7aacd43bd3b1?q=80&w=300&auto=format&fit=crop',
    specs: '黑色 / 运动表带 / 防水版',
    recipient: {
      name: '李四',
      phone: '139****9000',
      address: '北京市朝阳区建国路88号现代城5号楼1单元801'
    },
    logistics: {
      company: '京东物流',
      trackingNumber: 'JD1234987654321',
      status: '已签收',
      updates: [
        { time: '2025-07-12 16:30:00', content: '客户已签收，签收人：李四，感谢您使用京东物流' },
        { time: '2025-07-12 10:15:00', content: '快件已到达【朝阳区建国路站点】，快递员【王师傅，电话：13800000000】正在为您派送' },
        { time: '2025-07-11 18:45:00', content: '快件已到达【北京朝阳区】' },
        { time: '2025-07-11 06:20:00', content: '商家已发货' }
      ]
    },
    paymentMethod: 'USDT',
    orderSteps: [
      { title: '提交订单', time: '2025-07-10 11:23:45', completed: true },
      { title: '付款', time: '2025-07-10 11:24:30', completed: true },
      { title: '商家发货', time: '2025-07-11 06:20:00', completed: true },
      { title: '确认收货', time: '2025-07-12 16:30:00', completed: true },
      { title: '订单完成', time: '2025-07-12 16:30:00', completed: true }
    ]
  }
}

/**
 * 获取订单列表
 * @param {Object} params 查询参数 {status, keyword, page, pageSize}
 * @returns {Object} 订单列表数据
 */
export function getOrderList(params = {}) {
  let result = [...orderData]
  
  // 按状态筛选
  if (params.status && params.status !== 'all') {
    result = result.filter(order => order.status === params.status)
  }
  
  // 按关键词搜索
  if (params.keyword) {
    const keyword = params.keyword.toLowerCase()
    result = result.filter(order => {
      // 搜索订单号
      if (order.orderNumber.toLowerCase().includes(keyword)) {
        return true
      }
      
      // 搜索商品名称
      return order.items.some(item => 
        item.name.toLowerCase().includes(keyword)
      )
    })
  }
  
  // 分页处理
  const page = params.page || 1
  const pageSize = params.pageSize || 10
  const total = result.length
  const start = (page - 1) * pageSize
  const end = start + pageSize
  
  return {
    list: result.slice(start, end),
    pagination: {
      page,
      pageSize,
      total
    }
  }
}

/**
 * 获取订单详情
 * @param {string|number} id 订单ID
 * @returns {Object} 订单详情数据
 */
export function getOrderDetail(id) {
  // 先从订单列表中查找
  const orderFromList = orderData.find(order => order.id === id)
  if (orderFromList) {
    return orderFromList
  }
  
  // 再从详情数据中查找
  return orderDetailData[id] || null
}

/**
 * 取消订单
 * @param {string|number} id 订单ID
 * @returns {Object} 操作结果
 */
export function cancelOrder(id) {
  const order = orderData.find(order => order.id === id)
  if (!order) {
    return { success: false, message: '订单不存在' }
  }
  
  if (order.status !== 'pending') {
    return { success: false, message: '只有待付款订单可以取消' }
  }
  
  // 模拟更新订单状态
  order.status = 'cancelled'
  order.statusText = '已取消'
  order.cancelTime = new Date().toISOString().replace('T', ' ').substring(0, 19)
  order.cancelReason = '用户取消'
  
  return { success: true, message: '订单已取消' }
}

/**
 * 确认收货
 * @param {string|number} id 订单ID
 * @returns {Object} 操作结果
 */
export function confirmReceived(id) {
  const order = orderData.find(order => order.id === id)
  if (!order) {
    return { success: false, message: '订单不存在' }
  }
  
  if (order.status !== 'shipped') {
    return { success: false, message: '只有待收货订单可以确认收货' }
  }
  
  // 模拟更新订单状态
  order.status = 'completed'
  order.statusText = '已完成'
  
  // 更新订单步骤
  const now = new Date().toISOString().replace('T', ' ').substring(0, 19)
  const confirmStep = order.orderSteps.find(step => step.title === '确认收货')
  if (confirmStep) {
    confirmStep.time = now
    confirmStep.completed = true
  }
  
  const completeStep = order.orderSteps.find(step => step.title === '订单完成')
  if (completeStep) {
    completeStep.time = now
    completeStep.completed = true
  }
  
  // 更新物流状态
  if (order.logistics) {
    order.logistics.status = '已签收'
    order.logistics.updates.unshift({
      time: now,
      content: `客户已签收，签收人：${order.recipient.name}，感谢您使用${order.logistics.company}`
    })
  }
  
  return { success: true, message: '已确认收货' }
}

/**
 * 删除订单
 * @param {string|number} id 订单ID
 * @returns {Object} 操作结果
 */
export function deleteOrder(id) {
  const index = orderData.findIndex(order => order.id === id)
  if (index === -1) {
    return { success: false, message: '订单不存在' }
  }
  
  const order = orderData[index]
  if (order.status !== 'completed' && order.status !== 'cancelled') {
    return { success: false, message: '只有已完成或已取消的订单可以删除' }
  }
  
  // 模拟删除订单
  orderData.splice(index, 1)
  
  return { success: true, message: '订单已删除' }
}

/**
 * 支付订单
 * @param {string|number} id 订单ID
 * @param {Object} paymentData 支付信息
 * @returns {Object} 操作结果
 */
export function payOrder(id, paymentData) {
  const order = orderData.find(order => order.id === id)
  if (!order) {
    return { success: false, message: '订单不存在' }
  }
  
  if (order.status !== 'pending') {
    return { success: false, message: '只有待付款订单可以支付' }
  }
  
  // 模拟更新订单状态
  order.status = 'paid'
  order.statusText = '待发货'
  
  // 更新支付方式
  if (paymentData && paymentData.method) {
    order.paymentMethod = paymentData.method
  }
  
  // 更新订单步骤
  const now = new Date().toISOString().replace('T', ' ').substring(0, 19)
  const payStep = order.orderSteps.find(step => step.title === '付款')
  if (payStep) {
    payStep.time = now
    payStep.completed = true
  }
  
  return { success: true, message: '支付成功' }
}